//
//  WithdrawDepositViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/21.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "WithdrawDepositViewController.h"
#import "EditBankCardViewController.h"
#import "WithdrawDepositBottomView.h"
#import "WithdrawDepositAddBankCell.h"
#import "WithdrawDepositBankCell.h"
#import "WithdrawDepositInputCell.h"
#import "UserInfoModel.h"
NSString * const WithdrawDepositBottomViewIdentifier = @"WithdrawDepositBottomViewIdentifier";
NSString * const WithdrawDepositAddBankCellIdentifier = @"WithdrawDepositAddBankCellIdentifier";
NSString * const WithdrawDepositBankCellIdentifier = @"WithdrawDepositBankCellIdentifier";
NSString * const WithdrawDepositInputCellIdentifier = @"WithdrawDepositInputCellIdentifier";
@interface WithdrawDepositViewController ()

@property(nonatomic, strong) NSDictionary * bankInfo;
@property(nonatomic, copy) NSString * maxCash_price;//最大提现
@property(nonatomic, copy) NSString * min_cash;//最小
@property(nonatomic, copy) NSString * cash_price;//提现金额
@end

@implementation WithdrawDepositViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationItemTitle: @"提现"];

    [self requestBankInfo];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestBankInfo) name:@"UPATE_BANKINFO_NOTIFI" object:nil];
}
- (void)requestBankInfo{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Wallet/getBankInfo" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            if (responseObject[@"data"][@"bank"]) {
                strongSelf.bankInfo = responseObject[@"data"][@"bank"];
            }
            if ( responseObject[@"data"][@"cash_price"] ) {
                self.maxCash_price = responseObject[@"data"][@"cash_price"];
            }
            if ( responseObject[@"data"][@"min_cash"]) {
                self.min_cash =  responseObject[@"data"][@"min_cash"];
            }
            [strongSelf.tableView reloadData];
        }
    }];
    
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WithdrawDepositBankCell class]) bundle:nil] forCellReuseIdentifier:WithdrawDepositBankCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WithdrawDepositAddBankCell class]) bundle:nil] forCellReuseIdentifier:WithdrawDepositAddBankCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WithdrawDepositInputCell class]) bundle:nil] forCellReuseIdentifier:WithdrawDepositInputCellIdentifier];
    [self.tableView  registerNib:[UINib nibWithNibName:NSStringFromClass([WithdrawDepositBottomView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:WithdrawDepositBottomViewIdentifier];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 2  ;
 
    return rows;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 0;
    if (indexPath.row == 0 ) {
        if (self.bankInfo ) {
            height = 70;
        }else{
           height = 50;
        }
        
    }else if(indexPath.row == 1){
       height = 80;
    }
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if (indexPath.row == 0 ) {
        if (!self.bankInfo ) {
            
            WithdrawDepositAddBankCell * tempCell = [tableView dequeueReusableCellWithIdentifier:WithdrawDepositAddBankCellIdentifier];
            cell = tempCell;
        }else {
            WithdrawDepositBankCell * tempCell = [tableView dequeueReusableCellWithIdentifier:WithdrawDepositBankCellIdentifier];
            [tempCell setupBankInfo:self.bankInfo];
            cell = tempCell;
        }
       
    } else{
        WithdrawDepositInputCell * tempCell = [tableView dequeueReusableCellWithIdentifier:WithdrawDepositInputCellIdentifier];
        tempCell.inputBlock = ^(NSString *inputText) {
            NSLog(@"input==%@",inputText);
            self.cash_price = inputText;
        };
        cell = tempCell;
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = nil;
    WithdrawDepositBottomView * bottomView = (WithdrawDepositBottomView *)[self.tableView dequeueReusableHeaderFooterViewWithIdentifier:WithdrawDepositBottomViewIdentifier];
    [bottomView setupMaxNo:self.maxCash_price withMin:self.min_cash];
    bottomView.btnBlock = ^{
        [self submitBankInfo];
    };
    footerView = bottomView;
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        if (!self.bankInfo ) {
            [self gotoAddBankCard];
        }else {
            [self gotoChangeBankCard];
        }
    }
    
}

- (void)gotoAddBankCard{
    
    EditBankCardViewController * tempVC = [[EditBankCardViewController alloc]initWithType:BankCardVCType_add withBankInfo:nil];
    [self.navigationController pushViewController:tempVC animated:YES];
}
- (void)gotoChangeBankCard{
    
    EditBankCardViewController * tempVC = [[EditBankCardViewController alloc]initWithType:BankCardVCType_edit withBankInfo:self.bankInfo];
    [self.navigationController pushViewController:tempVC animated:YES];
}

- (void)submitBankInfo{
    if (!self.cash_price) {
        self.cash_price = @"0";
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    [params addEntriesFromDictionary:@{@"cash_price":self.cash_price}];
    [params addEntriesFromDictionary:@{@"user_bank_id":self.bankInfo[@"user_bank_id"]}];
    
    
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Wallet/addCash" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            [strongSelf showAlertView:@"提现申请成功,返回我的钱包" withConfirmAction:^(UIAlertAction *action) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"UPATE_WALLET_NOTIFI" object:nil];
                [strongSelf updateTableView];
            }];
        }
       
    }];
    
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UPATE_BANKINFO_NOTIFI" object:nil ];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
